package com.j.lemon.learn.basic.sanjiliandong;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lijunjun
 * @Date 2018-12-28 11:40
 * @Description
 */
public class SanjiDemo {


    public static void main(String[] args) {
        String s = "{\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"DICNAME\": \"PROVINCE\",\n" +
                "      \"CODE\": \"110000\",\n" +
                "      \"DESCRIBE\": \"北京市\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"CITY\",\n" +
                "      \"CODE\": \"110100\",\n" +
                "      \"DESCRIBE\": \"北京市\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"COUNTY\",\n" +
                "      \"CODE\": \"110101\",\n" +
                "      \"DESCRIBE\": \"东城区\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"COUNTY\",\n" +
                "      \"CODE\": \"110102\",\n" +
                "      \"DESCRIBE\": \"西城区\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"PROVINCE\",\n" +
                "      \"CODE\": \"410000\",\n" +
                "      \"DESCRIBE\": \"河南省\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"CITY\",\n" +
                "      \"CODE\": \"410800\",\n" +
                "      \"DESCRIBE\": \"焦作市\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"CITY\",\n" +
                "      \"CODE\": \"410700\",\n" +
                "      \"DESCRIBE\": \"新乡市\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"COUNTY\",\n" +
                "      \"CODE\": \"410804\",\n" +
                "      \"DESCRIBE\": \"解放区\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"COUNTY\",\n" +
                "      \"CODE\": \"410803\",\n" +
                "      \"DESCRIBE\": \"山阳区\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"COUNTY\",\n" +
                "      \"CODE\": \"410704\",\n" +
                "      \"DESCRIBE\": \"牧野区\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"DICNAME\": \"COUNTY\",\n" +
                "      \"CODE\": \"410703\",\n" +
                "      \"DESCRIBE\": \"凤泉区\"\n" +
                "    }\n" +
                "  ]\n" +
                "}";
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray jsonArray = jsonObject.getJSONArray("data");

        JSONArray provinces = new JSONArray();

        //开始循环每条数据
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            //先对省份操作
            if ("PROVINCE".equals(object.getString("DICNAME"))) {
                JSONObject province = new JSONObject();
                province.put("name", object.getString("DESCRIBE"));

                //省份开头3位
                String provinceStartStr = object.getString("CODE").substring(0, 3);
                JSONArray cities = new JSONArray();
                //循环所有数据，找到市
                for (int j = 0; j < jsonArray.size(); j++) {

                    JSONObject json = jsonArray.getJSONObject(j);
                    //如果是城市，并且code以当前省份的前三位（provinceStartStr）开头，则说明该市属于当前省，则put进去
                    if (("CITY".equals(json.getString("DICNAME")) && json.getString("CODE").startsWith(provinceStartStr))) {
                        JSONObject city = new JSONObject();
                        city.put("name", json.getString("DESCRIBE"));
                        //县区集合
                        List<String> countys = new ArrayList<>();
                        //市开头四位
                        String cityStartStr = json.getString("CODE").substring(0, 4);
                        //循环所有数据，找到县区
                        for (int k = 0; k < jsonArray.size(); k++) {
                            JSONObject county = jsonArray.getJSONObject(k);
                            //如果是县区，并且code以当前市的前四位（cityStartStr）开头，则说明该区县属于该市，add进去
                            if (("COUNTY".equals(county.getString("DICNAME")) && county.getString("CODE").startsWith(cityStartStr))) {
                                countys.add(county.getString("DESCRIBE"));
                            }
                        }
                        city.put("districtAndCounty", countys);
                        cities.add(city);
                    }
                }
                province.put("city", cities);
                provinces.add(province);
            }
        }

        System.out.println(provinces.toString());
    }
}
